package com.hzp.micromall.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzp.micromall.common.core.api.CommonPage;
import com.hzp.micromall.product.dao.PmsProductCategoryAttributeRelationDao;
import com.hzp.micromall.product.dao.PmsProductCategoryDao;
import com.hzp.micromall.product.dao.PmsProductDao;
import com.hzp.micromall.product.dto.PmsProductCategoryParam;
import com.hzp.micromall.product.dto.PmsProductCategoryWithChildrenItem;
import com.hzp.micromall.product.entity.PmsProduct;
import com.hzp.micromall.product.entity.PmsProductCategory;
import com.hzp.micromall.product.entity.PmsProductCategoryAttributeRelation;
import com.hzp.micromall.product.service.PmsProductCategoryAttributeRelationService;
import com.hzp.micromall.product.service.PmsProductCategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author houzipeng
 * @Date 2022/2/26 15:43
 * @Version 1.0
 * 商品分类Service实现类
 */
@Service
public class PmsProductCategoryServiceImpl implements PmsProductCategoryService {

    @Autowired
    private PmsProductCategoryDao productCategoryDao;
    @Autowired
    private PmsProductDao productDao;
    @Autowired
    private PmsProductCategoryAttributeRelationService productCategoryAttributeRelationService;

    @Override
    public int create(PmsProductCategoryParam pmsProductCategoryParam) {
        PmsProductCategory productCategory = new PmsProductCategory();
        BeanUtil.copyProperties(pmsProductCategoryParam, productCategory);
        productCategory.setProductCount(0);
        // 无父类的时候设置为一级分类
        setCategoryLevel(productCategory);
        int count = productCategoryDao.insert(productCategory);
        // 创建筛选属性关联
        List<Long> productAttributeIdList = pmsProductCategoryParam.getProductAttributeIdList();
        if (CollUtil.isNotEmpty(productAttributeIdList)){
            insertRelationList(productCategory.getId(), productAttributeIdList);
        }
        return count;
    }

    @Override
    public int delete(Long id) {
        return productCategoryDao.deleteById(id);
    }

    @Override
    public int update(Long id, PmsProductCategoryParam pmsProductCategoryParam) {
        PmsProductCategory productCategory = new PmsProductCategory();
        productCategory.setId(id);
        BeanUtil.copyProperties(pmsProductCategoryParam, productCategory);
        setCategoryLevel(productCategory);
        //更新商品分类时要更新商品中的名称
        PmsProduct product = new PmsProduct();
        product.setProductCategoryName(productCategory.getName());
        LambdaQueryWrapper<PmsProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PmsProduct::getProductCategoryId, id);
        productDao.update(product, wrapper);
        //同时更新筛选属性的信息
        LambdaQueryWrapper<PmsProductCategoryAttributeRelation> relationWrapper = new LambdaQueryWrapper<>();
        relationWrapper.eq(PmsProductCategoryAttributeRelation::getProductCategoryId, id);
        productCategoryAttributeRelationService.remove(relationWrapper);
        if(CollUtil.isNotEmpty(pmsProductCategoryParam.getProductAttributeIdList())){
            insertRelationList(id,pmsProductCategoryParam.getProductAttributeIdList());
        }
        return productCategoryDao.updateById(productCategory);
    }

    @Override
    public int updateNavStatus(List<Long> ids, Integer navStatus) {
        PmsProductCategory productCategory = new PmsProductCategory();
        productCategory.setNavStatus(navStatus);
        LambdaQueryWrapper<PmsProductCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(PmsProductCategory::getId, ids);
        return productCategoryDao.update(productCategory, wrapper);
    }

    @Override
    public int updateShowStatus(List<Long> ids, Integer showStatus) {
        PmsProductCategory productCategory = new PmsProductCategory();
        productCategory.setShowStatus(showStatus);
        LambdaQueryWrapper<PmsProductCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(PmsProductCategory::getId, ids);
        return productCategoryDao.update(productCategory, wrapper);
    }

    @Override
    public CommonPage<PmsProductCategory> getPage(Long parentId, Integer pageSize, Integer pageNum) {
        Page<PmsProductCategory> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<PmsProductCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PmsProductCategory::getParentId, parentId)
                .orderByDesc(PmsProductCategory::getSort);
        productCategoryDao.selectPage(page, wrapper);
        return new CommonPage<>(page.getCurrent(), page.getTotal(), page.getSize(), page.getRecords());
    }

    @Override
    public PmsProductCategory getItem(Long id) {
        return productCategoryDao.selectById(id);
    }

    @Override
    public List<PmsProductCategoryWithChildrenItem> listWithChildren() {
        return productCategoryDao.listWithChildren();
    }

    /**
     * 根据分类的parentId设置分类的level
     */
    private void setCategoryLevel(PmsProductCategory productCategory) {
        //没有父分类时为一级分类
        if (productCategory.getParentId() == 0) {
            productCategory.setLevel(0);
        } else {
            //有父分类时选择根据父分类level设置
            PmsProductCategory parentCategory = productCategoryDao.selectById(productCategory.getParentId());
            if (parentCategory != null) {
                productCategory.setLevel(parentCategory.getLevel() + 1);
            } else {
                productCategory.setLevel(0);
            }
        }
    }

    /**
     * 批量插入商品分类与筛选属性关系表
     * @param productCategoryId 商品分类id
     * @param productAttributeIdList 相关商品筛选属性id集合
     */
    private void insertRelationList(Long productCategoryId, List<Long> productAttributeIdList) {
        List<PmsProductCategoryAttributeRelation> relationList = new ArrayList<>();
        for (Long productAttrId : productAttributeIdList) {
            PmsProductCategoryAttributeRelation relation = new PmsProductCategoryAttributeRelation();
            relation.setProductAttributeId(productAttrId);
            relation.setProductCategoryId(productCategoryId);
            relationList.add(relation);
        }
        productCategoryAttributeRelationService.saveBatch(relationList);
    }
}
